package com.szmg.wishwalls.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.szmg.common.BaseDao;
import com.szmg.wishwalls.dto.VoteDetailDto;
import com.szmg.wishwalls.dto.VoteSortDto;
import com.szmg.wishwalls.model.Discuss;
import com.szmg.wishwalls.model.OrderProductionRelation;
import com.szmg.wishwalls.model.Program;
import com.szmg.wishwalls.model.VoteDetail;
import com.szmg.wishwalls.model.WishOrder;

@Component("wishOrderDao")
public class WishOrderDao extends BaseDao{
	
	public void saveWishOrder(WishOrder wishOrder){
		writerSqlSession.insert("com.szmg.wishwalls.dao.WishOrderDao.saveWishOrder", wishOrder);
	}
	
	public void saveOrderProductionRelation(OrderProductionRelation orderProductionRelation){
		writerSqlSession.insert("com.szmg.wishwalls.dao.WishOrderDao.saveOrderProductionRelation", orderProductionRelation);
	}
	
	public void saveVoteDetail(VoteDetail voteDetail){
		writerSqlSession.insert("com.szmg.wishwalls.dao.WishOrderDao.saveVoteDetail", voteDetail);
	}
	
	public void saveDiscuss(Discuss discuss){
		writerSqlSession.insert("com.szmg.wishwalls.dao.WishOrderDao.saveDiscuss", discuss);
	}
	
	/**
	 * 根据心愿单id获取所有第一次评论，按时间顺序倒序排序
	 * @param wishOrderId
	 * @return
	 */
	public List<Discuss> queryRootDiscussByWishOrderId(String wishOrderId){
		return this.readSqlSession.selectList("com.szmg.wishwalls.dao.WishOrderDao.queryRootDiscussByWishOrderId", wishOrderId);
	}
	
	/**
	 * 获取心愿单的所有评论记录
	 * @param wishOrderId
	 * @return
	 */
	public List<Discuss> queryDiscussByWishOrderId(String wishOrderId){
		return this.readSqlSession.selectList("com.szmg.wishwalls.dao.WishOrderDao.queryDiscussByWishOrderId", wishOrderId);
	}
	
	/**
	 * 获取某个产品对应的投票者明细
	 * @param wishOrderId
	 * @param productionId
	 * @return
	 */
	public List<VoteDetail> queryVoteDetailsByRelationId(String wishOrderId,String productionId){
		Map<String,String> map = new HashMap<String,String>();
		map.put("wishOrderId", wishOrderId);
		map.put("productionId", productionId);
		return this.readSqlSession.selectList("com.szmg.wishwalls.dao.WishOrderDao.queryVoteDetailsByRelationId", map);
	}
	
	/**
	 * 查询心愿单和产品的关联关系
	 * @param wishOrderId
	 * @param productionId
	 * @return
	 */
	public OrderProductionRelation queryOrderProductionRelation(String wishOrderId,String productionId){
		Map<String,String> map = new HashMap<String,String>();
		map.put("wishOrderId", wishOrderId);
		map.put("productionId", productionId);
		return this.readSqlSession.selectOne("com.szmg.wishwalls.dao.WishOrderDao.queryOrderProductionRelation", map);
	}
	
	/**
	 * 根据评论id获取评论
	 * @param discussId
	 * @return
	 */
	public Discuss queryDiscussById(String discussId){
		return this.readSqlSession.selectOne("com.szmg.wishwalls.dao.WishOrderDao.queryDiscussById", discussId);
	}
	
	/**
	 * 根据心愿单id获取心愿单
	 * @param wishOrderId
	 * @return
	 */
	public WishOrder queryWishOrderById(String wishOrderId){
		return this.readSqlSession.selectOne("com.szmg.wishwalls.dao.WishOrderDao.queryWishOrderById", wishOrderId);
	}
	
	/**
	 * 根据活动code获取活动实体
	 * @param code
	 * @return
	 */
	public Program queryProgramByCode(String code){
		return this.readSqlSession.selectOne("com.szmg.wishwalls.dao.WishOrderDao.queryProgramByCode", code);
	}
	
	/**
	 * 根据微信号查询本次活动的心愿单
	 * @param wishOrder
	 * @param programCode
	 * @return
	 */
	public WishOrder queryWishOrderByWechat(String wechatId,String programCode){
		Map<String,String> map = new HashMap<String,String>();
		map.put("wechatId", wechatId);
		map.put("programCode", programCode);
		return this.readSqlSession.selectOne("com.szmg.wishwalls.dao.WishOrderDao.queryWishOrderByWechat", map);
	}
	
	/**
	 * 获取指定产品在本次活动中的前十名
	 * @param programId
	 * @param produtionId
	 * @return
	 */
	public List<VoteSortDto> queryWishOrderDto(String programId,String produtionId){
		Map<String,String> map = new HashMap<String,String>();
		map.put("programId", programId);
		map.put("produtionId", produtionId);
		return this.readSqlSession.selectList("com.szmg.wishwalls.dao.WishOrderDao.queryWishOrderDto", map);
	}
	
	/**
	 * 更新星愿单的心愿
	 * @param wishOrderId
	 * @param wishDeclaration
	 */
	public void updateWishOrder(String wishOrderId,String wishDeclaration){
		Map<String,String> map = new HashMap<String,String>();
		map.put("wishOrderId", wishOrderId);
		map.put("wishDeclaration", wishDeclaration);
		writerSqlSession.update("com.szmg.wishwalls.dao.WishOrderDao.updateWishOrder", map);
	}
	
	public VoteDetail queryVoteDetailsByWechatId(VoteDetailDto voteDetailDto){
		Map<String,String> map = new HashMap<String,String>();
		map.put("wishOrderId", voteDetailDto.getWishOrderId());
		map.put("productionId", voteDetailDto.getProductionId());
		map.put("wechatId", voteDetailDto.getWechatId());
		return readSqlSession.selectOne("com.szmg.wishwalls.dao.WishOrderDao.queryVoteDetailsByWechatId", map);
	}
	
	public void updateVoteCountById(String relationId){
		writerSqlSession.update("com.szmg.wishwalls.dao.WishOrderDao.updateVoteCountById", relationId);
	}
	
	public List<OrderProductionRelation> queryOrderProductionRelationList(String wishOrderId){
		Map<String,String> map = new HashMap<String,String>();
		map.put("wishOrderId", wishOrderId);
		return this.readSqlSession.selectList("com.szmg.wishwalls.dao.WishOrderDao.queryOrderProductionRelationList", map);
	}
	
	public List<VoteDetail> queryVoteDetailsByRelationAndOpenId(String relationId,String openId){
		Map<String,String> map = new HashMap<String,String>();
		map.put("relationId", relationId);
		map.put("openId", openId);
		return this.readSqlSession.selectList("com.szmg.wishwalls.dao.WishOrderDao.queryVoteDetailsByRelationAndOpenId", map);
	}
	
	public void superAllProduction(List<String> relationIds){
		Map<String,List<String>> map = new HashMap<String,List<String>>();
		map.put("relationIds", relationIds);
		writerSqlSession.update("com.szmg.wishwalls.dao.WishOrderDao.superAllProduction", map);
	}
}
